package com.funcourse.demo.api.restful.io.response.busi;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.funcourse.demo.model.CompeteRoom;
import com.funcourse.demo.model.UserWithScore;
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.LinkedList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CompeteRoomSocketVO {
    //original fields
    String id;
    Integer currentQuestionNO = 0;
    CompeteRoom.RoomStatus roomStatus = CompeteRoom.RoomStatus.CREATED;
    //customized fields
    List<UserWithScore> topNUsers = new LinkedList<>();
    UserWithScore myScore;
    Integer userCount;
    Boolean questionAllAnswered = false;
    public CompeteRoomSocketVO(CompeteRoom source,String toUserId,Integer topUserAmount,Boolean myScoreDetails){
        super();
        BeanUtils.copyProperties(source,this);
        final List<UserWithScore> sourceUserWithScoreList = source.getUserWithScoreList();
        this.userCount = sourceUserWithScoreList.size();

        //build topNUsers
        final List<UserWithScore> orginalUserWithScoreList = sourceUserWithScoreList
            .subList(0, userCount > topUserAmount ? topUserAmount : userCount);
        //减少带宽
        orginalUserWithScoreList.forEach(userWithScore -> {
            UserWithScore us = new UserWithScore();
            BeanUtils.copyProperties(userWithScore,us);
            us.setScoreDetails(null);
            this.topNUsers.add(us);
        });

        //build myScore
        for(UserWithScore userWithScore :sourceUserWithScoreList ){
            if(userWithScore.getId().equals(toUserId) ){
                myScore = new UserWithScore();
                BeanUtils.copyProperties(userWithScore,myScore);
                //减少带宽
                if(!myScoreDetails){
                    myScore.setScoreDetails(null);
                }
                break;
            }
        }

    }
}
